#!/bin/bash


#####确保程序在root下运行###############
if [[ $UID -ne 0 || $uid -ne 0 ]];then
	echo  -e "\033[36mPlease Run the $0 under root\033[0m"
	exit
fi


function dns()
{

######开放防火墙############
firewall-cmd --zone=public --add-port=53/tcp --permanent
firewall-cmd --zone=public --add-port=53/udp --permanent
firewall-cmd --reload
systemctl restart named

#####查看是否安装过######
rpm -q bind

if [ $? -eq 0 ];then
	echo  -e "\033[36mPlease uninstall and run the program after it has been installed\033[0m"	
		exit
else
    yum install -y bind*
fi


########更改named配置文件##############
sed -i '13s/listen-on port 53 { 127.0.0.1; };/listen-on port 53 { any; };/g' /etc/named.conf
sed -i '21s/allow-query     { localhost; };/allow-query     { any; };/g' /etc/named.conf


###修改DNS的配置文件添加正向逆向解析域###
read -p "Please enter your computer name:" cn
read -p "Please type the first three digits of your IP backwards:" ip

echo  " 
zone \"$cn\" IN {
        type master;
        file \"$cn.zone\";
        allow-update { none; };
};

zone \"$ip.in-addr.arpa\" IN {
        type master;
        file \"$ip.in-addr.arpa.zone\";
        allow-update { none; };
};

" >> /etc/named.rfc1912.zones

########配置正向解析########
cp -p /var/named/named.localhost /var/named/$cn.zone

sed -i '9d'  /var/named/cent7.com.zone

read -p "Please enter your DNS server IP:" dip

echo "        A       $dip" >> /var/named/$cn.zone

PS3="Do you want to input(1|2):"

select i in  "input" "out" 

do 

    case $i in
        
        input )
        read -p "Please enter a network name:" nn
        read -p "Please enter the record mode(A/PTR/CNAME/MX/NS):" mode
        read -p "Please enter the corresponding IP:" cip
        echo "$nn     $mode       $cip" >>  /var/named/$cn.zone
        ;;

        out )
        break
        ;;

        * )
	    echo -e "\033[32mUsage: {$0 1=input|2=out} \033[0m"
	;;
    esac

done

#####配置逆向解析######
cp -p /var/named/named.loopback /var/named/$ip.in-addr.arpa.zone

sed -i '11d' /var/named/$ip.in-addr.arpa.zone

echo "        A       $cn."

PS3="Do you want to input lookback(1|2):"

select j in  "input" "out" 

do 

    case $j in

        input )
        read -p "Please enter the host number:" hn
        read -p "Please enter the website:" web
        echo "$hn     PTR       $web." >>  /var/named/$ip.in-addr.arpa.zone
        ;;

        out )
        break
        ;;

        * )
	    echo -e "\033[32mUsage: {$0 1=intput|2=out} \033[0m"
    
    esac

done

systemctl restart named

> /etc/resolv.conf

echo "nameserver $dip"

systemctl restart named

}

function dp()
{
function dhcp()

{

#####查看是否安装过######
rpm -q dhcp

if [ $? -eq 0 ];then
		echo  -e "\033[36mPlease uninstall and run the program after it has been installed\033[0m"
		exit
else
    yum install -y dhcp
fi

read -p "Please input your IP:" IP
read -p "Please input your GATEWAY:" GATEWAY
read -p "Please input your Starting IP:" StartingIP
read -p "Please input your Termination of IP:" TerminationIP
read -p "Please input your network card:" nc

oneIP=`echo $IP |awk -F. '{print $1}'`
twoIP=`echo $IP |awk -F. '{print $2}'`
treeIP=`echo $IP |awk -F. '{print $3}'`

i=`echo "$oneIP.$twoIP.$treeIP.0"`

echo "
option domain-name-servers $IP;
default-lease-time 600;
max-lease-time 7200;
subnet $i       
netmask 255.255.255.0 
 {
    range  $StartingIP $TerminationIP;
    option routers $GATEWAY;
 }

 " >> /etc/dhcp/dhcpd.conf

 echo "DHCPDARGS="$nc" " >> /etc/sysconfig/dhcpd

 systemctl restart dhcpd

} 


function nnc()
{

   read -p "Please Enter your new network card name:" nnc

   uuid=`uuidgen $nnc`

echo "
TYPE="Ethernet"
PROXY_METHOD="none"
BROWSER_ONLY="no"
BOOTPROTO="dhcp"
DEFROUTE="yes"
IPV4_FAILURE_FATAL="no"
IPV6INIT="yes"
IPV6_AUTOCONF="yes"
IPV6_DEFROUTE="yes"
IPV6_FAILURE_FATAL="no"
IPV6_ADDR_GEN_MODE="stable-privacy"
NAME="$nnc"
UUID="$uuid"
DEVICE="$nnc"
ONBOOT="yes"
" > /etc/sysconfig/network-scripts/ifcfg-$nnc

systemctl restart dhcpd
systemctl restart network

}

function Menu()
{
	echo -e "\033[32mPlease enter the service you want\033[0m"	
	echo "1)DHCP"
	echo "2)Network adapter configuration"	
    echo "3)EXIT"
}

echo -e "\033[32mPlease enter the service you want \033[0m"

PS3="select your will exec Menu(1|2|3):"

select j in "DHCP" "NNC" "Exit"

do

 	case $j in
		DHCP )
		echo -e "\033[32mwait insatll DHCP server....\033[0m"
		dhcp
		Menu
		;;

		NNC )
		echo -e "\033[32mWait to configure the network card....\033[0m"
		nnc
		Menu
		;;

		Exit )
		break
		;;

		* )
		echo -e "\033[32mUsage: {$0 DHCP|Set the nic} \033[0m"
		Menu
		;;
    esac

done
}

function smb()
{

rpm -q samba

if [ $? -eq 0 ];then
		echo  -e "\033[36mPlease uninstall and run the program after it has been installed\033[0m"
		exit
else
    yum install -y samba
fi

firewall-cmd --zone=public --add-port=139/tcp --permanent
firewall-cmd --zone=public --add-port=445/tcp --permanent
firewall-cmd --zone=public --add-port=137/udp --permanent
firewall-cmd --zone=public --add-port=138/udp --permanent
firewall-cmd --reload
systemctl restart firewalld.service

read -p "Please input user:" input

useradd $input

smbpasswd -a $input

echo "

[$input]
	comment = $input
	path = /home/$input 
	browseable = Yes
	writable = Yes
	available = Yes
	valid users = $input
	write list = $input
" >> /etc/samba/smb.conf

setenforce 0

systemctl restart smb

}


function M()
{
	echo -e "\033[32mPlease enter the service you want\033[0m"	
	echo "1)DHCP"
	echo "2)DNS"
    echo "3)SMB"	
    echo "4)EXIT"
}

echo -e "\033[32mPlease enter the service you want \033[0m"

PS3="select your will exec Menu(1|2|3|4):"

select j in "dns" "dhcp" "smb" "Exit"

do

 	case $j in
		dhcp )
		echo -e "\033[32mwait insatll DHCP server....\033[0m"
		dp
		M
		;;

		dns )
		echo -e "\033[32mwait install dns server\033[0m"
		dns
		M
		;;

        smb )
		echo -e "\033[32mwait install smb server\033[0m"
		smb
		M
		;;

		Exit )
		exit
		;;

		* )
		echo -e "\033[32mUsage: {$0 DNS|DHCP|SMB} \033[0m"
		Menu
		;;
    esac

done